package com.ruoyi.report.service.impl;

import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.oiltank.domain.OilTank;
import com.ruoyi.oiltank.service.IOilTankService;
import com.ruoyi.report.domain.ReportDcDetail;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.report.domain.ReportScDetail;
import com.ruoyi.report.mapper.ReportScMapper;
import com.ruoyi.report.domain.ReportSc;
import com.ruoyi.report.service.IReportScService;

/**
 * 供应站Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-15
 */
@Service
public class ReportScServiceImpl implements IReportScService 
{
    @Autowired
    private ReportScMapper reportScMapper;
    @Autowired
    private IOilTankService oilTankService;
    /**
     * 查询供应站
     * 
     * @param id 供应站ID
     * @return 供应站
     */
    @Override
    public ReportSc selectReportScById(Long id)
    {
        return reportScMapper.selectReportScById(id);
    }

    /**
     * 查询供应站列表
     * 
     * @param reportSc 供应站
     * @return 供应站
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<ReportSc> selectReportScList(ReportSc reportSc)
    {
        List<String> dateQuery = reportSc.getDateQuery();
        if (CollectionUtil.isNotEmpty(dateQuery)){
            String start = dateQuery.get(0);
            String end = dateQuery.get(1);
            reportSc.setStart(start);
            reportSc.setEnd(end);
        }
        return reportScMapper.selectReportScList(reportSc);
    }

    @Override
    public List<ReportSc> selectReportScListNoPage(ReportSc reportSc)
    {
        List<String> dateQuery = reportSc.getDateQuery();
        if (CollectionUtil.isNotEmpty(dateQuery)){
            String start = dateQuery.get(0);
            String end = dateQuery.get(1);
            reportSc.setStart(start);
            reportSc.setEnd(end);
        }
        return reportScMapper.selectReportScList(reportSc);
    }


    public Integer countTodayRecordsByDeptId( Long deptId){
        return reportScMapper.countTodayRecordsByDeptId(deptId);
    }
    /**
     * 新增供应站
     * 
     * @param reportSc 供应站
     * @return 结果
     */
    @Transactional
    @Override
    public int insertReportSc(ReportSc reportSc)
    {
        reportSc.setCreateTime(DateUtils.getNowDate());
        int rows = reportScMapper.insertReportSc(reportSc);
        insertReportScDetail(reportSc);

        List<ReportScDetail> reportScDetailList = reportSc.getReportScDetailList();
        for (ReportScDetail reportScDetail : reportScDetailList) {
            Long tankId = reportScDetail.getTankId();
            BigDecimal tankHeight = reportScDetail.getTankHeight();
            Integer tankStatus = reportScDetail.getTankStatus();
            BigDecimal tankWeight = reportScDetail.getTankWeight();
            OilTank oilTank = oilTankService.selectOilTankById(tankId);
            if (ObjectUtil.isNull(oilTank)){
                throw new RuntimeException("查询不到储罐信息");
            }
            oilTank.setId(tankId);
            oilTank.setStatus(tankStatus);
            oilTank.setWeight(tankWeight);
            oilTank.setHeight(tankHeight);
            oilTankService.updateOilTank(oilTank);

        }

        return rows;
    }

    /**
     * 修改供应站
     * 
     * @param reportSc 供应站
     * @return 结果
     */
    @Transactional
    @Override
    public int updateReportSc(ReportSc reportSc)
    {
        reportSc.setUpdateTime(DateUtils.getNowDate());
        reportScMapper.deleteReportScDetailByScId(reportSc.getId());
        insertReportScDetail(reportSc);

        List<ReportScDetail> reportScDetailList = reportSc.getReportScDetailList();
        for (ReportScDetail reportScDetail : reportScDetailList) {
            Long tankId = reportScDetail.getTankId();
            BigDecimal tankHeight = reportScDetail.getTankHeight();
            Integer tankStatus = reportScDetail.getTankStatus();
            BigDecimal tankWeight = reportScDetail.getTankWeight();
            OilTank oilTank = oilTankService.selectOilTankById(tankId);
            if (ObjectUtil.isNull(oilTank)){
                throw new RuntimeException("查询不到储罐信息");
            }
            oilTank.setId(tankId);
            oilTank.setStatus(tankStatus);
            oilTank.setWeight(tankWeight);
            oilTank.setHeight(tankHeight);
            oilTankService.updateOilTank(oilTank);

        }
        return reportScMapper.updateReportSc(reportSc);
    }

    /**
     * 批量删除供应站
     * 
     * @param ids 需要删除的供应站ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteReportScByIds(Long[] ids)
    {
        reportScMapper.deleteReportScDetailByScIds(ids);
        return reportScMapper.deleteReportScByIds(ids);
    }

    /**
     * 删除供应站信息
     * 
     * @param id 供应站ID
     * @return 结果
     */
    @Override
    public int deleteReportScById(Long id)
    {
        reportScMapper.deleteReportScDetailByScId(id);
        return reportScMapper.deleteReportScById(id);
    }

    /**
     * 新增供应站油罐信息详情信息
     * 
     * @param reportSc 供应站对象
     */
    public void insertReportScDetail(ReportSc reportSc)
    {
        List<ReportScDetail> reportScDetailList = reportSc.getReportScDetailList();
        Long id = reportSc.getId();
        if (StringUtils.isNotNull(reportScDetailList))
        {
            List<ReportScDetail> list = new ArrayList<ReportScDetail>();
            for (ReportScDetail reportScDetail : reportScDetailList)
            {
                reportScDetail.setScId(id);
                list.add(reportScDetail);
            }
            if (list.size() > 0)
            {
                reportScMapper.batchReportScDetail(list);
            }
        }
    }
}
